
$().ready(function() {
    layui.use('upload','form', function() {
        var $ = layui.jquery,form = layui.form
            , upload = layui.upload;
        //选完文件后不自动上传
        upload.render({
            elem: '#test3'
            ,url: ctx +'module/terminal/readExcelBatchAdd' //改成您自己的上传接口
            ,auto: false
            //,multiple: true
            ,exts: 'xls|xlsx' //只允许上传压缩文件
            ,bindAction: '#test9'
            ,done: function(res){
                if (res.code == 0) {
                    parent.layer.msg("上传成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }
            }
        });
    });
    validateRule();

    $("#checkCountUnit").ajaxChosen({
        category : 'clean-cycle-type'
    });
    $("#cleanCountUnit").ajaxChosen({
        category : 'clean-cycle-type'
    });
    $("#terminalType").ajaxChosen({
        category : 'terminal-type'
    });

    $('#checkPoints').summernote({
        height : '220px',
        lang : 'zh-CN',
        callbacks: {
            onImageUpload: function(files, editor, $editable) {
                console.log("onImageUpload");
            }
        }
    });
});

$.validator.setDefaults({
    submitHandler : function() {
        update();
    }
});

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            shopId:{  required : true  },
            terminalName : { required : true  },
            terminalNum : { required : true  },
            terminalType : { required : true  },
            checkCount : { required : true  },
            checkCountUnit : { required : true  },
        },
        messages : {
            shopId : {
                required : icon + "请选择企业"
            },
            terminalName : {
                required : icon + "请输入设备名称"
            },
            terminalNum : {
                required : icon + "请输入设备编号"
            },
            terminalType : {
                required : icon + "请选择设备工段"
            },
            checkCount : {
                required : icon + "请输入周期次数"
            },
            checkCountUnit : {
                required : icon + "请选择周期单位"
            }
        }
    })
}

function showCheckPoint() {
    var isShowCheckPoints = $("#isShowCheckPoint").val();
    console.log('isShowCheckPoints',isShowCheckPoints);
    $("#divCheckPoints").removeClass("hidden");
    if(isShowCheckPoints == '0'){
        $('#divCheckPoints').addClass("hidden");
    }else if(isShowCheckPoints == '1'){
        $("#divCheckPoints").removeClass("hidden");
    }
}

function update() {
    var url = ctx + "module/terminal/" + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    $.ajax({
        cache : true,
        type : "POST",
        url : url,
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

}
